package com.gk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.pojo.*;
import com.eight.util.TimeUtil;
import com.eight.vo.ComResult;
import com.eight.vo.TbUserCouponVo;
import com.eight.vo.UserCouponVo;
import com.gk.dao.TbIntegrationHistoryDao;
import com.gk.dao.TbUserCouponDao;
import com.gk.service.CouponHistoryService;
import com.gk.service.TbIntegrationHistoryService;
import com.gk.service.TbUserCouponService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: GK
 * @date: 2023/3/1 9:43
 * @version:No1.0
 */
@Service
public class TbUserCouponServiceImpl extends ServiceImpl<TbUserCouponDao, TbUserCoupon> implements TbUserCouponService {

    @Resource
    private TbUserCouponDao tbUserCouponDao;

    @Resource
    private CouponHistoryService couponHistoryService;

    /**
     * 根据用户和优惠卷的内容进行添加数据
     * @param tbUserCouponVo 需要添加的数据
     * @return 响应前端
     */
    @Override
    public ComResult insertUserCoupon(TbUserCouponVo tbUserCouponVo) {
        // 用户优惠券的对象
        TbUserCoupon tbUserCoupon = tbUserCouponVo.getTbUserCoupon();
        // 优惠券的List
        List<TbCoupon> tbCoupons = tbUserCouponVo.getTbCoupons();
        // 现在要生成用户优惠券的List
        List<TbUserCoupon> tbUserCouponList = tbCoupons.stream().map(e ->
                new TbUserCoupon()
                        .setUserName(tbUserCoupon.getUserName())
                        .setUserId(tbUserCoupon.getUserId())
                        .setCouponId(Math.toIntExact(e.getId()))
                        .setStartTime(e.getStartTime())
                        .setStopTime(e.getEndTime())
                        .setCouponName(e.getName())

        ).collect(Collectors.toList());
        //制作优惠卷信息对象
        List<CouponHistory> couponHistoryStream = tbCoupons.stream().map(e -> new CouponHistory()
                .setCouponId(e.getName())
                .setCustomerName(tbUserCoupon.getUserName())
                .setCreateTime(TimeUtil.now(""))
                .setUseStatus(0)
                .setGetType(0)
        ).collect(Collectors.toList());
         couponHistoryService.saveBatch(couponHistoryStream);
        return saveBatch(tbUserCouponList)?new ComResult(2000,"插入成功"):new ComResult(2000,"插入失败");
    }

    /**
     * 分页条件查询用户和优惠卷
     * @param userCouponVo 查询条件
     * @param pageIndex 当前页
     * @param pageSize 页中数据量
     * @return
     */
    @Override
    public ComResult selectAllUserCoupon(UserCouponVo userCouponVo, Integer pageIndex, Integer pageSize) {
        Page<TbUserCoupon> page = new Page<>(pageIndex, pageSize);
        page=tbUserCouponDao.selectAllUserCoupon(page,userCouponVo);
        if(page!=null){
            return new ComResult(2000,"查询成功",page);
        }else{
            return new ComResult(5000,"查询失败");
        }
    }

    /**
     * 根据id删除客户和id信息
     * @param id 条件 条件
     * @return 响应前端
     */
    @Override
    public ComResult deleteFindId(Integer id) {
        int i = tbUserCouponDao.deleteById(id);
        if(i>0){
           return new ComResult(2000,"删除成功");
        }else{
            return new ComResult(5000,"删除失败");
        }
    }

    /**
     * 根据账号查询此人拥有的优惠卷
     * @param username 账号
     * @return 所有查到的数据
     */
    @Override
    public List<TbUserCoupon> selectAllUser(String username) {
        QueryWrapper<TbUserCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",username);
        List<TbUserCoupon> tbUserCoupons = tbUserCouponDao.selectList(wrapper);
        return tbUserCoupons;
    }

    /**
     * 根据用户优惠卷信息插入信息
     * @param tbUserCoupon 用户优惠卷信息
     * @return
     */
    @Override
    public int inserUserCon(TbUserCoupon tbUserCoupon) {
        int insert = tbUserCouponDao.insert(tbUserCoupon);
        return insert;
    }

    /**
     * 根据账号查询用户和优惠卷
     * @param username 账号
     * @return 1
     */
    @Override
    public ComResult selectUserCon(String username) {
        List<TbUserCoupon> tbUserCoupons = tbUserCouponDao.selectUserCon(username);
        List<TbUserCoupon> resultCollect = tbUserCoupons.stream().map(e -> {
            e.setStime(e.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000L);
            e.setEtime(e.getStopTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000L);
            return e;
        }).collect(Collectors.toList());
        if(tbUserCoupons!=null){
            return new ComResult(2000,"查询成功",resultCollect);
        }else{
            return new ComResult(5000,"查询失败");
        }
    }

    /**
     * 根据账号和优惠卷id删除用户优惠卷
     * @param username 账号
     * @param couponId 优惠卷id
     * @return 1
     */
    @Override
    public int deleteUserCon(String username, Integer couponId) {
    int i=   tbUserCouponDao.deleteUserCon(username,couponId);
    return i;
    }
}
